<template>
  <div class="app-container">
    <el-row :gutter="20" v-if="!workflowRecordDetailsOpen">
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="流程编号" prop="serialNumber">
            <el-input v-model="queryParams.serialNumber" placeholder="请输入流程编号" size="small" clearable style="width: 200px"/>
          </el-form-item>
          <el-form-item label="流程名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入流程名称" size="small" clearable style="width: 200px"/>
          </el-form-item>
          <el-form-item label="发起时间">
            <el-date-picker @change="handleQuery" v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary"  icon="el-icon-edit-outline" size="mini" @click="handleWorkflow" :disabled="single">处理</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="queryInProcessWorkflowRecordPageResList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table ref="multipleTable" :stripe="true" v-loading="loading" :data="dataList" style="width: 100%"  @selection-change="handleSelectionChange"
                  @sort-change='tableSortChange' :default-sort="{prop: 'sort', order: 'descending'}">
          <el-table-column type="selection" align="center"/>
          <el-table-column label="流程发起人" align="left" width="250" >
            <template slot-scope="scope">
              {{scope.row.startLoginName}}({{scope.row.startUserName}}) <el-tag type="info" size="mini" v-if="scope.row.startDeptName">{{scope.row.startDeptName}}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="流程编号" align="left" prop="serialNumber" width="200" />
          <el-table-column label="流程名称" align="left" prop="projectName"/>
          <el-table-column label="任务ID" align="left" prop="taskId" width="100" />
          <el-table-column label="任务节点" align="left" prop="taskName"/>
          <el-table-column label="发起时间" align="center" prop="createTime" width="200" />
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit-outline" @click="handleWorkflow(scope.row)">处理</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="queryInProcessWorkflowRecordPageResList"/>
      </el-col>
    </el-row>

    <!-- 工作流详情 -->
    <div v-if="workflowRecordDetailsOpen">
      <handleWorkflow
        @close="closeHandleWorkflowForm"
        :key="workflowRecordDetailsTaskId"
        :record-id="workflowRecordDetailsRecordId"
        :project-id="workflowRecordDetailsProjectId"
        :task-id="workflowRecordDetailsTaskId"
      />
    </div>
  </div>
</template>

<script>
  import handleWorkflow from './view/handleWorkflow.vue'
  import {queryInProcessWorkflowRecordPageResList} from '@/api/workflow/workflowRecord'
  //默认查询参数
  let defaultQueryParams = {
    projectName: undefined,
    startTime: undefined,
    endTime: undefined,
    serialNumber:undefined,
    page: 1,
    rows: 10
  }
  export default {
    name: 'inProcessWorkflowAdmin',
    components: {
      handleWorkflow
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        //操作日志
        dataList: [],
        // 日期范围
        dateRange: [],
        //发送状态
        sendStatusCodeList: [],
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //工作流详情弹窗打开
        workflowRecordDetailsOpen:false,
        //工作流详申请记录id
        workflowRecordDetailsRecordId:"",
        //工作流任务id
        workflowRecordDetailsTaskId:"",
        //工作流项目ID
        workflowRecordDetailsProjectId:"",
        //列信息
        columns: []
      }
    },
    created() {
      this.workflowRecordDetailsOpen = false;
      this.queryInProcessWorkflowRecordPageResList()
    },
    methods: {
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.taskId)
        this.single = selection.length !== 1;
        this.multiple = !selection.length
      },
      //排序
      tableSortChange(column) {
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.queryInProcessWorkflowRecordPageResList()
      },
      /** 查询工作流在办记录列表 */
      queryInProcessWorkflowRecordPageResList() {
        this.loading = true
        queryInProcessWorkflowRecordPageResList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
            this.dataList = res.data.records
            this.total = res.data.total
          }
        ).finally(() => {
          //关闭加载特效
          this.loading = false
        })
      },

      /** 流程处理 */
      handleWorkflow(row){
        row = row.isTrusted ? this.$refs.multipleTable.selection[0] : row;
        this.workflowRecordDetailsOpen = true;
        this.workflowRecordDetailsRecordId = row.recordId;
        this.workflowRecordDetailsTaskId = row.taskId;
        this.workflowRecordDetailsProjectId = row.projectId;
      },

      //关闭处理工作流界面
      closeHandleWorkflowForm(isRefresh) {
        this.workflowRecordDetailsOpen = false;
        this.ids = [];
        this.single = true;
        this.multiple = true;
        if (isRefresh){
          this.queryInProcessWorkflowRecordPageResList()
        }
      },

      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.dateRange = []
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.queryInProcessWorkflowRecordPageResList()
      }
    }
  }
</script>
